package com.example.springbootquartz.config;
import com.example.springbootquartz.job.QuartzJob1;
import com.example.springbootquartz.job.QuartzJob2;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import java.util.Date;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA2022.3.2
 * @Author: Tenghw
 * @Date: 2023/08/05  11:48
 * @Description: 任务调度器
 */
@Configuration
public class MyQuartzScheduler {

    /**
     * 任务调度器
     */
    @Autowired
    private Scheduler scheduler;

    /**
     * QuartzJob1
     */
    public void startQuartzJob1(Scheduler scheduler) throws SchedulerException {
        // 1.通过JobBuilder构建JobDetail实例，且JobDetail规定只能是实现Job接口的实现类实例
        // JobDetail是具体的Job实例
        JobDetail jobDetail = JobBuilder.newJob(QuartzJob1.class).withIdentity("job1", "group1").build();
        // 2.基于cron表达式构建触发器实例(每5秒执行一次)
        CronScheduleBuilder cronSchedulerBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
        // 2.1.表达式触发器cronTrigger继承于Trigger
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity("job1", "group1")
                .withSchedule(cronSchedulerBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * QuartzJob2
     */
    public void startQuartzJob2(Scheduler scheduler) throws SchedulerException {
        // 1.通过JobBuilder构建JobDetail实例，且JobDetail规定只能是实现Job接口的实现类实例
        // JobDetail是具体的Job实例
        JobDetail jobDetail = JobBuilder.newJob(QuartzJob2.class).withIdentity("job2", "group2").build();
        // 2.基于cron表达式构建触发器实例(每8秒执行一次)
        CronScheduleBuilder cronSchedulerBuilder = CronScheduleBuilder.cronSchedule("0/8 * * * * ?");
        // 2.1.表达式触发器cronTrigger继承于Trigger
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity("job2", "group2")
                .withSchedule(cronSchedulerBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 开始执行所有任务
     */
    public void startAllJobs() throws SchedulerException {
        startQuartzJob1(scheduler);
        startQuartzJob2(scheduler);
        scheduler.start();
    }

    /**
     * 获取job信息
     */
    public String getJobInfo(String jobName, String jobGroupName) throws SchedulerException {
        //TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroupName);
        TriggerKey triggerKey = new TriggerKey(jobName, jobGroupName);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        String info = String.format("time:%s,srate:%s", cronTrigger.getCronExpression(), scheduler.getTriggerState(triggerKey).name());
        return info;
    }

    /**
     * 修改某个job任务的执行时间
     */
    public boolean modifyJobTime(String jobName, String jobGroupName, String cronNewTime) throws SchedulerException {
        Date date = null;
        //TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroupName);
        TriggerKey triggerKey = new TriggerKey(jobName, jobGroupName);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        if (cronTrigger == null) {
            return false;
        }
        String oldTime = cronTrigger.getCronExpression();
        if (!oldTime.equalsIgnoreCase(cronNewTime)) {
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronNewTime);
            CronTrigger cronTrigger1 = TriggerBuilder.newTrigger()
                    .withIdentity(jobName, jobGroupName)
                    .withSchedule(cronScheduleBuilder).build();
            date = scheduler.rescheduleJob(triggerKey, cronTrigger1);
        }
        return Objects.nonNull(date);
    }

    /**
     * 暂停所有job定时任务
     */
    public void pauserAllJobs() throws SchedulerException {
        scheduler.pauseAll();
    }

    /**
     * 暂停某个job定时任务
     */
    public void pauserJob(String jobName, String jobGroupName) throws SchedulerException {
        JobKey jobKey = new JobKey(jobName, jobGroupName);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (Objects.isNull(jobDetail)) {
            return;
        }
        scheduler.pauseJob(jobKey);
    }

    /**
     * 恢复所有job定时任务
     */
    public void resumeAllJobs() throws SchedulerException {
        scheduler.resumeAll();
    }

    /**
     * 恢复某个job定时任务
     */
    public void resumJob(String jobName, String jobGroupName) throws SchedulerException {
        JobKey jobKey = new JobKey(jobName, jobGroupName);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (Objects.isNull(jobDetail)) {
            return;
        }
        scheduler.resumeJob(jobKey);
    }

    /**
     * 删除某个job定时任务
     */
    public void deleteJob(String jobName, String jobGroupName) throws SchedulerException {
        JobKey jobKey = new JobKey(jobName, jobGroupName);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (Objects.isNull(jobDetail)) {
            return;
        }
        scheduler.deleteJob(jobKey);
    }


}



